package com.example.administrator.kotlintabviewpagerfragment.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.RecyclerView.Adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import com.example.administrator.kotlintabviewpagerfragment.R

/**
 * Created by sswl on 2017/7/7.
 */
class RecChlidAdapter (var mCtx :Context,var mList: List<String>): Adapter<RecyclerView.ViewHolder>() {

    val TYE_ONE : Int =1
    val TYE_TWO : Int =2

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
        when(viewType){
            TYE_ONE -> {
                return TypeOneHolder(LayoutInflater.from(mCtx).inflate(R.layout.type_one_item,parent,false))
            }
            else -> {
                return TypeTwoHolder(LayoutInflater.from(mCtx).inflate(R.layout.type_four_item,parent,false))
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        if (holder is TypeTwoHolder){
            bindTwoHolder(holder,position)
        }else if (holder is  TypeOneHolder){
            bindOneHolder(holder,position)
        }
    }
    override fun getItemViewType(position: Int): Int {
        if (position<3){
            return TYE_ONE
        }
        else{
            return TYE_TWO
        }
    }
    override fun getItemCount(): Int {
        return mList.size
    }
    /**
     * position >= 3 时 的条目点击事件
     * */
    private fun bindTwoHolder(holder: TypeTwoHolder, position: Int) {
        holder?.ll.setOnClickListener {
            Listener?.onClick(position)
        }

    }
    /*
    * position  <3  点击事件
    * */
    private fun bindOneHolder(holder: TypeOneHolder, position: Int) {
        holder?.llone.setOnClickListener {
            Listener?.onClick(position)
        }

    }

    class TypeOneHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
        var llone:LinearLayout=itemView!!.findViewById(R.id.llone) as  LinearLayout
    }

    class TypeTwoHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
        var ll:LinearLayout=itemView!!.findViewById(R.id.ll_detail_item) as  LinearLayout
    }


    interface OnItemClickListener{
        fun onClick(position: Int)
    }
    var Listener : OnItemClickListener?=null
    fun setOnClick(litener :OnItemClickListener ){
        this.Listener=litener
    }
}